<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;
use function Couchbase\fastlzCompress;

class IndexDistributor extends BaseModel
{
    //定义表名

    protected $name = 'longbing_card_distributor_application';

    /**
     * @param $dis
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 申请列表
     */
    public function getList($dis, $page = 10)
    {
        $whereLike = '';
        $where['a.uniacid'] = $dis['uniacid'];
        if (isset($dis['str']) && !empty($dis['str'])) {
            $whereLike = "nickName like '%" . $dis['str'] . "%' or a.user_id like '%" . $dis['str'] . "%' or a.phone like '%" . $dis['str'] . "%'";
        }
        if (isset($dis['status']) && $dis['status']!=-2) {
            $where['a.status'] = $dis['status'];
        }
        $whereTime="";
        $start=strtotime($dis['start']);
        $end=strtotime($dis['end'])+3600*24;
        if (isset($dis['end']) && $dis['end'] && isset($dis['start']) && $dis['start']) {
            $whereTime = 'a.create_time > '. $start.' and a.create_time < '.$end;
        }
        $data = Db::name('longbing_card_distributor_application')
            ->alias('a')
            ->where($whereTime)
            ->where($whereLike)
            ->leftJoin('longbing_card_user b', 'a.user_id = b.id')
            ->where($where)
            ->field(['a.*,b.avatarUrl,b.nickName'])
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();
        return $data;
    }


    /**
     * @author 郑文军
     * @DataTime: 2020/5/19 13:20
     * @功能说明:统计数据
     */
    public function getCount($where)
    {
        $data['all'] = Db::name('longbing_card_distributor_application')
            ->where($where)
            ->count();
        $where['status'] = -1;
        $data['refuse'] = Db::name('longbing_card_distributor_application')
            ->where($where)
            ->count();
        $where['status'] = 1;
        $data['adopt'] = Db::name('longbing_card_distributor_application')
            ->where($where)
            ->count();
        $where['status'] = 0;
        $data['wait'] = Db::name('longbing_card_distributor_application')
            ->where($where)
            ->count();
        return $data;
    }

    /**
     * @param $where
     * @功能说明: 获取审核详情
     * @author 郑文军
     * @DataTime: 2020/5/19 13:30
     */
    public function getInfo($where)
    {
        $data = Db::name('longbing_card_distributor_application')
            ->alias('a')
            ->where($where)
            ->leftJoin('longbing_card_user b', 'a.user_id = b.id')
            ->field(['a.*,b.avatarUrl,b.nickName'])
            ->order('a.id desc')
            ->find();
        //获取授权手机号
        $phone= Db::name('longbing_card_user_phone')
            ->alias("a")
            ->where(['user_id'=>$data['user_id']])
            ->order("id desc")
            ->find();
        $data['user_phone']=$phone?$phone['phone']:'';
        return $data;
    }

    /**
     * @param $where
     * @param $data
     * @功能说明: 审核申请
     * @author 郑文军
     * @DataTime: 2020/5/19 13:39
     */
    public function updateDistributor($where, $data)
    {
        Db::startTrans();
        $res = Db::name('longbing_card_distributor_application')
            ->where($where)
            ->update($data);
        if ($res && $data['status'] == 1) {
            $distributor = Db::name('longbing_card_distributor_application')
                ->where($where)
                ->find();
            $res = Db::name('longbing_card_user')
                ->where(['uniacid' => $where['uniacid'], 'id' => $distributor['user_id']])
                ->update(['distribution' => 2]);
            //更新用户为销售员状态
        }
        Db::commit();
        return $res ? true : false;
    }


    /**
     * @author 郑文军
     * @DataTime: 2020/5/19 15:12
     * @功能说明:添加用户申请
     */
    public function addDistributor($where,$data)
    {
        $distributor = Db::name('longbing_card_distributor_application')->order("id desc")->where($where)->find();
        //5天不能重复申请
        /*if ($distributor && $distributor['create_time'] + 5 * 3600 * 24 > time()) {*/
//        if ($distributor && $distributor['create_time'] + 60*2 > time()) {
//            return ['msg' => "2分钟内不能重复提交审核", 'code' => 0];
//        }
        if ($distributor && $distributor['create_time'] + 5 * 3600 * 24 > time()) {
            return ['msg' => "5天内不能重复提交审核", 'code' => 0];
        }
        //校验用户是员工
        $whereUser = ['id' => $where['user_id']];
        $user = Db::name('longbing_card_user')->where($whereUser)->find();

        if ($user['distribution'] == 2) {
            return ['msg' => "你已经是分销员", 'code' => 0];
        }
        if ($user['is_staff'] == 1) {
            return ['msg' => "公司员工不能申请分销员", 'code' => 0];
        }

        //获取分销员自动审核状态
        /*$setting = Db::name('longbing_card_config')->where(['uniacid' => $where['uniacid']])->find();
        if ($setting['distribution_user_examine'] == 1) {
            Db::name('longbing_card_user')->where($whereUser)->update(['pid'=>$user['from_id']]);
        }*/

        $data['create_time'] = time();
        $data['user_id'] = $where['user_id'];
        $data['uniacid'] = $where['uniacid'];

        $data['auditOpinion'] = !empty($data['auditOpinion'])?$data['auditOpinion']:'';

        $res = Db::name('longbing_card_distributor_application')->insert($data);
        return $res ? ['msg' => "申请成功", 'code' => 1] : ['msg' => "申请失败", 'code' => 0];
    }


    /**
     * @param $where
     * @功能说明: 获取当前审核状态
     * @author 郑文军
     * @DataTime: 2020/5/26 14:29
     */
    public function getDistributorStatus($where){
        $distributor = Db::name('longbing_card_distributor_application')->order("id desc")->where($where)->find();
        $is_reply=0;
        if ($distributor && $distributor['create_time'] + 5 * 3600 * 24 < time()) {
            $is_reply=1;
        }
        $data['isReply']=$is_reply;
        $data['status']=$distributor?$distributor['status']:-2;

        $data['auditOpinion']=$distributor?$distributor['auditOpinion']:'';


        return $data;
    }
}